<!DOCTYPE html>


  <html class="dark page-post">


<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>Mocha+Chai+Istanbul单元测试 | Poetry&#39;s Blog</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="JavaScript,测试,">
  

  <meta name="description" content="一、简介 Istanbul是JavaScript程序的代码覆盖率工具 Mocha是一种测试框架，也就是运行测试的工具。用descibe和it方法，来定义test suit，为不同的测试分组。 mocha本身并不提供assert断言，所以要提供更加有表现力的断言，可以搭配chai使用，当然也可以使用nodejs提供的assert模块  二、配置测试环境安装相应的依赖包 npm install --s">
<meta name="keywords" content="JavaScript,测试">
<meta property="og:type" content="article">
<meta property="og:title" content="Mocha+Chai+Istanbul单元测试">
<meta property="og:url" content="http://blog.poetries.top/2017/10/22/unit-test-mocha-chai/index.html">
<meta property="og:site_name" content="Poetry&#39;s Blog">
<meta property="og:description" content="一、简介 Istanbul是JavaScript程序的代码覆盖率工具 Mocha是一种测试框架，也就是运行测试的工具。用descibe和it方法，来定义test suit，为不同的测试分组。 mocha本身并不提供assert断言，所以要提供更加有表现力的断言，可以搭配chai使用，当然也可以使用nodejs提供的assert模块  二、配置测试环境安装相应的依赖包 npm install --s">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://poetries1.gitee.io/img-repo/2019/10/586.png">
<meta property="og:updated_time" content="2020-08-15T04:25:31.938Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Mocha+Chai+Istanbul单元测试">
<meta name="twitter:description" content="一、简介 Istanbul是JavaScript程序的代码覆盖率工具 Mocha是一种测试框架，也就是运行测试的工具。用descibe和it方法，来定义test suit，为不同的测试分组。 mocha本身并不提供assert断言，所以要提供更加有表现力的断言，可以搭配chai使用，当然也可以使用nodejs提供的assert模块  二、配置测试环境安装相应的依赖包 npm install --s">
<meta name="twitter:image" content="https://poetries1.gitee.io/img-repo/2019/10/586.png">

  

  
    <link rel="icon" href="/favicon.ico">
  

  <link href="/css/styles.css?v=c114cbeddx" rel="stylesheet">
<link href="/css/other.css?v=c114cbeddx" rel="stylesheet">


  
    <link rel="stylesheet" href="/css/personal-style.css">
  

  

  
  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?40b1f89aa80f2527b3db779c6898c879";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>


  
  <script type="text/javascript">
	(function(){
	    var bp = document.createElement('script');
	    var curProtocol = window.location.protocol.split(':')[0];
	    if (curProtocol === 'https') {
	        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
	    }
	    else {
	        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
	    }
	    var s = document.getElementsByTagName("script")[0];
	    s.parentNode.insertBefore(bp, s);
	})();
  </script>



  
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.3.0/css/font-awesome.min.css">
  

  <!-- 聊天系统 -->
  
    
   <link type="text/css" rel="stylesheet" href="/renxi/default.css">
   <style>
      #modal {
        position: static !important;
      }
      .filter {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: #fe5757;
        animation: colorChange 30s ease-in-out infinite;
        animation-fill-mode: both;
        mix-blend-mode: overlay;
      }
  
      @keyframes colorChange {
        0%, 100% {
            opacity: 0;
        }
        50% {
            opacity: .9;
        }
      }
   </style>
</head>
</html>
<body>
  
  
    <span id="toolbox-mobile" class="toolbox-mobile">导航</span>
  

  <div class="post-header CENTER">
   
  <div class="toolbox">
    <a class="toolbox-entry" href="/">
      <span class="toolbox-entry-text">导航</span>
      <i class="icon-angle-down"></i>
      <i class="icon-home"></i>
    </a>
    <ul class="list-toolbox">
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/archives/"
            rel="noopener noreferrer"
            target="_self"
            >
            博客
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/categories/"
            rel="noopener noreferrer"
            target="_self"
            >
            分类
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/tags/"
            rel="noopener noreferrer"
            target="_self"
            >
            标签
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/search/"
            rel="noopener noreferrer"
            target="_self"
            >
            搜索
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/link/"
            rel="noopener noreferrer"
            target="_self"
            >
            友链
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/about/"
            rel="noopener noreferrer"
            target="_self"
            >
            关于
          </a>
        </li>
      
    </ul>
  </div>


</div>


  <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录<i class="iconfont toc-title" style="display:inline-block;color:#87998d;width:20px;height:20px;">&#xf004b;</i></strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#一、简介"><span class="toc-text">一、简介</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#二、配置测试环境"><span class="toc-text">二、配置测试环境</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#三、编写测试代码"><span class="toc-text">三、编写测试代码</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#四、断言库的用法"><span class="toc-text">四、断言库的用法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#五、持续集成"><span class="toc-text">五、持续集成</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#六、Mocha的命令行参数"><span class="toc-text">六、Mocha的命令行参数</span></a></li></ol>
  </div>
  




<div class="content content-post CENTER">
   <!-- canvas 彩带 -->
<canvas id="evanyou" width="1302" height="678" style="position: fixed;width: 100%;height: 100%;top: 0;left:0;z-index:-1;"></canvas>

<article id="post-unit-test-mocha-chai" class="article article-type-post" itemprop="blogPost">
  <header class="article-header" style="position:relative;">
    <h1 class="post-title">Mocha+Chai+Istanbul单元测试</h1>

    <div class="article-meta">
      <span>
        <i class="icon-calendar"></i>
        <span>2017.10.22</span>
      </span>

      
        <span class="article-author">
          <i class="icon-user"></i>
          <span>Poetry</span>
        </span>
      

      
  <span class="article-category">
    <i class="icon-list"></i>
    <a class="article-category-link" href="/categories/Front-End/">Front-End</a>
  </span>



      

      
      <i class="fa fa-eye"></i> 
        <span id="busuanzi_container_page_pv">
           &nbsp热度 <span id="busuanzi_value_page_pv">
           <i class="fa fa-spinner fa-spin"></i></span>℃
        </span>
      
      
       
          <span class="post-count">
            <i class="fa fa-file-word-o"></i>&nbsp
            <span>字数统计 1.4k字</span>
          </span>

          <span class="post-count">
            <i class="fa fa-columns"></i>&nbsp
            <span>阅读时长 5分</span>
          </span>
      
      
    </div>

    <i class="iconfont" id="toc-eye" style="display:inline-block;color:#b36619;position:absolute;top:0;right:0;cursor:pointer;
    font-size: 24px;">&#xe61c;</i>

  </header>

  <div class="article-content">
    
      <div id="container">
        <h2 id="一、简介"><a href="#一、简介" class="headerlink" title="一、简介"></a>一、简介</h2><ul>
<li><code>Istanbul</code>是<code>JavaScript</code>程序的代码覆盖率工具</li>
<li><code>Mocha</code>是一种测试框架，也就是运行测试的工具。用<code>descibe</code>和<code>it</code>方法，来定义<code>test suit</code>，为不同的测试分组。</li>
<li><code>mocha</code>本身并不提供<code>assert</code>断言，所以要提供更加有表现力的断言，可以搭配<code>chai</code>使用，当然也可以使用<code>nodejs</code>提供的<code>assert</code>模块</li>
</ul>
<h2 id="二、配置测试环境"><a href="#二、配置测试环境" class="headerlink" title="二、配置测试环境"></a>二、配置测试环境</h2><p><strong>安装相应的依赖包</strong></p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">npm install --save-dev mocha chai  istanbul</span><br></pre></td></tr></table></figure>
<ul>
<li>安装完成之后，在<code>package.json</code>文件的<code>scripts</code>下，添加执行测试和测试覆盖率检查的命令</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  </span><br><span class="line">  <span class="string">"scripts"</span>:&#123;</span><br><span class="line">    <span class="string">"cover"</span>: <span class="string">"istanbul cover _mocha -- -R spec --timeout 5000 --recursive"</span>,</span><br><span class="line">    <span class="string">"cover:check"</span>: <span class="string">"istanbul check-coverage"</span>,</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>注意，<code>window</code>下必须要这样才可以执行<code>cover</code></li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="string">"cover"</span>: <span class="string">"istanbul cover C:\Users\Administrator\Desktop\test\node_modules\mocha\bin\_mocha --reporter test/mocha.js"</span></span><br></pre></td></tr></table></figure>
<ul>
<li>运行<code>npm run cover</code>和<code>npm run cover:check</code>，就可以生成测试报告，前者生成测试报告，后者则是检查测试覆盖率是否达到要求</li>
</ul>
<p><img src="https://poetries1.gitee.io/img-repo/2019/10/586.png" alt></p>
<p><strong>配置Istanbul</strong></p>
<blockquote>
<p><code>istanbul</code>相关的执行参数，可以在命令行下执行时传递参数来制定，也可以在<code>yaml</code>格式的<code>.istanbul.yml</code>文件中配置。简单贴出一些重要的配置项</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">instrumentation:</span><br><span class="line">  root: .   # 执行的根目录</span><br><span class="line">  extensions:</span><br><span class="line">    - .js   # 检查覆盖率的文件扩张名</span><br><span class="line">  excludes: [<span class="string">'**/benchmark/**'</span>]</span><br><span class="line"></span><br><span class="line">  ... ...</span><br><span class="line"></span><br><span class="line">reporting:</span><br><span class="line">  print: summary</span><br><span class="line">  reports: [lcov, text, html, text-summary] # 生成报告的格式</span><br><span class="line">  dir: ./coverage   # 生成报告保存的目录</span><br><span class="line">  watermarks:       # 在不同覆盖率下会显示使用不同颜色</span><br><span class="line">    statements: [<span class="number">80</span>, <span class="number">95</span>]</span><br><span class="line">    ... ...</span><br><span class="line">check:</span><br><span class="line">  global:</span><br><span class="line">    statements: <span class="number">100</span></span><br><span class="line">    branches: <span class="number">100</span></span><br><span class="line">    lines: <span class="number">100</span></span><br><span class="line">    functions: <span class="number">100</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>最后的<code>check</code>是项目要通过覆盖率检查需要达到的测试覆盖率，测试覆盖率包括四个维度，分别是语句覆盖率、分支覆盖率、行覆盖率和函数覆盖率。如果达不到设定的指标，在执行的时候会报错，项目的测试就无法通过自动化的持续集成</p>
</blockquote>
<h2 id="三、编写测试代码"><a href="#三、编写测试代码" class="headerlink" title="三、编写测试代码"></a>三、编写测试代码</h2><blockquote>
<ul>
<li>利用<code>chai</code>提供的<code>expect</code>断言，我们可以用<code>BDD</code>的方式，写出更加符合代码预期行为的测试用例.</li>
<li>通常，测试脚本与所要测试的源码脚本同名，但是后缀名为<code>.test.js</code>（表示测试）或者<code>.spec.js</code>（表示规格）。比如，<code>add.js</code>的测试脚本名字就是<code>add.test.js</code></li>
</ul>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123;should, expect, assert&#125; = <span class="built_in">require</span>(<span class="string">'chai'</span>);</span><br><span class="line"></span><br><span class="line">describe(<span class="string">'#math'</span>, () =&gt; &#123;</span><br><span class="line">  describe(<span class="string">'add'</span>, () =&gt; &#123;</span><br><span class="line">    it(<span class="string">'should return 5 when 2 + 3'</span>, () =&gt; &#123;</span><br><span class="line">      expect(add(<span class="number">2</span>, <span class="number">3</span>), <span class="number">5</span>);</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    it(<span class="string">'should return 5 when 2 + 3'</span>, () =&gt; &#123;</span><br><span class="line">      expect(add(<span class="number">2</span>, <span class="number">-3</span>), <span class="number">-1</span>);</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;);</span><br><span class="line"></span><br><span class="line">  describe(<span class="string">'mul'</span>, () =&gt; &#123;</span><br><span class="line">    it(<span class="string">'should return 6 when 2 * 3'</span>, () =&gt; &#123;</span><br><span class="line">      expect(mul(<span class="number">2</span>, <span class="number">3</span>), <span class="number">6</span>);</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;);</span><br><span class="line"></span><br><span class="line">  describe(<span class="string">'cover'</span>, () =&gt; &#123;</span><br><span class="line">    it(<span class="string">'should return 1 when cover(2, 1)'</span>, () =&gt; &#123;</span><br><span class="line">      expect(cover(<span class="number">2</span>, <span class="number">1</span>)).to.equal(<span class="number">1</span>);</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    it(<span class="string">'should return 3 when cover(1, 2)'</span>, () =&gt; &#123;</span><br><span class="line">      expect(cover(<span class="number">1</span>, <span class="number">2</span>)).to.equal(<span class="number">3</span>);</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    it(<span class="string">'should return 4 when cover(2, 2)'</span>, () =&gt; &#123;</span><br><span class="line">      expect(cover(<span class="number">2</span>, <span class="number">2</span>)).to.equal(<span class="number">4</span>);</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<ul>
<li>测试脚本里面应该包括一个或多个<code>describe</code>块，每个<code>describe</code>块应该包括一个或多个<code>it</code>块</li>
<li><code>describe</code>中的名字是自己定义的，为了方便查看</li>
<li><code>describe</code>块称为”测试套件”（<code>test suite</code>），表示一组相关的测试。它是一个函数，第一个参数是测试套件的名称（”加法函数的测试”），第二个参数是一个实际执行的函数</li>
<li>it块称为”测试用例”（<code>test case</code>），表示一个单独的测试，是测试的最小单位</li>
</ul>
<h2 id="四、断言库的用法"><a href="#四、断言库的用法" class="headerlink" title="四、断言库的用法"></a>四、断言库的用法</h2><blockquote>
<p>所谓”断言”，就是判断源码的实际执行结果与预期结果是否一致，如果不一致就抛出一个错误</p>
</blockquote>
<ul>
<li>所有的测试用例（<code>it</code>块）都应该含有一句或多句的断言。它是编写测试用例的关键。断言功能由断言库来实现，<code>Mocha</code>本身不带断言库，所以必须先引入断言库</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> expect = <span class="built_in">require</span>(<span class="string">'chai'</span>).expect;</span><br></pre></td></tr></table></figure>
<ul>
<li>断言库有很多种，<code>Mocha</code>并不限制使用哪一种。上面代码引入的断言库是<code>chai</code>，并且指定使用它的<code>expect</code>断言风格。<code>chai</code>包含了几种风格，如<code>should</code> <code>expect</code> <code>assert</code>。<code>expect</code>断言的优点是很接近自然语言.</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 相等或不相等</span></span><br><span class="line">expect(<span class="number">4</span> + <span class="number">5</span>).to.be.equal(<span class="number">9</span>);</span><br><span class="line">expect(<span class="number">4</span> + <span class="number">5</span>).to.be.not.equal(<span class="number">10</span>);</span><br><span class="line">expect(foo).to.be.deep.equal(&#123; <span class="attr">bar</span>: <span class="string">'baz'</span> &#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 布尔值为true</span></span><br><span class="line">expect(<span class="string">'everthing'</span>).to.be.ok;</span><br><span class="line">expect(<span class="literal">false</span>).to.not.be.ok;</span><br><span class="line"></span><br><span class="line"><span class="comment">// typeof</span></span><br><span class="line">expect(<span class="string">'test'</span>).to.be.a(<span class="string">'string'</span>);</span><br><span class="line">expect(&#123; <span class="attr">foo</span>: <span class="string">'bar'</span> &#125;).to.be.an(<span class="string">'object'</span>);</span><br><span class="line">expect(foo).to.be.an.instanceof(Foo);</span><br><span class="line"></span><br><span class="line"><span class="comment">// include</span></span><br><span class="line">expect([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]).to.include(<span class="number">2</span>);</span><br><span class="line">expect(<span class="string">'foobar'</span>).to.contain(<span class="string">'foo'</span>);</span><br><span class="line">expect(&#123; <span class="attr">foo</span>: <span class="string">'bar'</span>, <span class="attr">hello</span>: <span class="string">'universe'</span> &#125;).to.include.keys(<span class="string">'foo'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// empty</span></span><br><span class="line">expect([]).to.be.empty;</span><br><span class="line">expect(<span class="string">''</span>).to.be.empty;</span><br><span class="line">expect(&#123;&#125;).to.be.empty;</span><br><span class="line"></span><br><span class="line"><span class="comment">// match</span></span><br><span class="line">expect(<span class="string">'foobar'</span>).to.match(<span class="regexp">/^foo/</span>);</span><br></pre></td></tr></table></figure>
<ul>
<li>基本上，<code>expect</code>断言的写法都是一样的。头部是<code>expect</code>方法，尾部是断言方法，比如<code>equal</code>、<code>a/an</code>、<code>ok</code>、<code>match</code>等。两者之间使用<code>to</code>或<code>to.be</code>连接</li>
<li>如果<code>expect</code>断言不成立，就会抛出一个错误。事实上，只要不抛出错误，测试用例就算通过。</li>
</ul>
<h2 id="五、持续集成"><a href="#五、持续集成" class="headerlink" title="五、持续集成"></a>五、持续集成</h2><blockquote>
<p>完成所有代码之后，我们可以将代码发布到<code>github</code>，然后使用持续集成工具<code>travis</code>检查代码，将生成的测试报告上传到<code>coverall</code>上，这样就可以在项目中显示项目状态和测试覆盖率的badges</p>
</blockquote>
<ul>
<li>持续集成是一种软件开发流程</li>
<li>频繁将代码集成到主干</li>
<li>每次集成都通过自动化的构建来验证</li>
<li>尽早发现错误</li>
<li><p>防止防止大幅偏离主干</p>
</li>
<li><p>通常的<code>nodejs</code>项目<code>.travis.yml</code>配置如下</p>
</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">language: node_js</span><br><span class="line">node_js:</span><br><span class="line">  - <span class="string">"6"</span></span><br><span class="line">  - <span class="string">"8"</span></span><br><span class="line">brancher:</span><br><span class="line">  only:</span><br><span class="line">    - <span class="string">"dev"</span></span><br><span class="line">    - <span class="string">"master"</span></span><br><span class="line">install:</span><br><span class="line">  - <span class="string">"npm install"</span></span><br><span class="line">  - <span class="string">"npm install -g codecov"</span></span><br><span class="line">script:</span><br><span class="line">  - <span class="string">"npm run cover"</span></span><br><span class="line">  - <span class="string">"codecov"</span></span><br></pre></td></tr></table></figure>
<ul>
<li>测试代码预览 <a href="https://github.com/poetries/test" target="_blank" rel="noopener">https://github.com/poetries/test</a></li>
</ul>
<h2 id="六、Mocha的命令行参数"><a href="#六、Mocha的命令行参数" class="headerlink" title="六、Mocha的命令行参数"></a>六、Mocha的命令行参数</h2><ul>
<li><code>--help</code>或<code>-h</code>参数，用来查看<code>Mocha</code>的所有命令行参数</li>
<li><code>--reporter, -R</code> <code>--reporter</code>参数用来指定测试报告的格式，默认是<code>spec</code>格式</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">$ mocha</span><br><span class="line"># 等同于</span><br><span class="line">$ mocha --reporter spec</span><br></pre></td></tr></table></figure>

      </div>
    
  </div>

</article>

<button class="assist-btn2 circle" id="assist_btn2" title="点亮屏幕" style="left: 27px; top: 152px;">
  <i class="iconfont" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>
<button class="assist-btn1 circle" id="assist_btn1" title="关闭屏幕亮度" style="left: 27px; top: 152px;">
  <i class="iconfont toc-title" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>


<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>	

<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
<script>
  const btw = new BTWPlugin();
  btw.init({
    id: "container",
    blogId: "22699-1592137983091-414",
    name: "前端进阶之旅",
    qrcode: "https://poetries1.gitee.io/img-repo/2020/06/qrcode.jpg",
    keyword: "3a3b3c",
  });
</script>

<script type="text/javascript">

// white theme
var body = {color: "#555", background: "#000"};
var a_tag = {color: "#222"};
var header = { background: "#222"};
var logo_line_i = {background: "#222"};
// var post_code = {background: "#eee", color: "#222"};

function switch_theme() {
 $("body").css(body);
 $("a:not('.links-of-author-item a, .site-state-item a, .site-state-posts a, .feed-link a, .motion-element a, .post-tags a, .show-commit-cls a, #donate_board a')").css(a_tag);
 $(".header, .footer").css(header);
 $(".logo-line-before i, .logo-line-after i").css(logo_line_i);
 //$(".post code").css(post_code);
 $("#idhyt-surprise-ball #idhyt-surprise-ball-animation .drag").css(a_tag);
 $(".post-title-link, .posts-expand .post-meta, .post-comments-count, .disqus-comment-count, .post-category a, .post-nav-next a, .post-nav-item a").css(a_tag);
 
 // $("code").css({color: '#c5c8c6', background: '#1d1f21'});
 //$("#assist_btn1").hide(1500);
}

$(function () {
$("#assist_btn2").css("display","none");
 $("#assist_btn1").click(function() {
     switch_theme();
$("div#toc.toc-article").css({
 "background":"#eaeaea",
 "opacity":1
});
$(".toc-article ol").show();
$("#toc.toc-article .toc-title").css("color","#a98602");
$("#assist_btn1").css("display","none");
$("#assist_btn2").css("display","block");
 });
$("#assist_btn2").click(function() {
$("#assist_btn2").css("display","none");
$("#assist_btn1").css("display","block");
$("body").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
     $(".header, .footer").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
$(".toc-article ol").toggle(1000);
 });
});


//背景随机

var Y, O, E, L, B, C, T, z, N, S, A, I;
!function() {
var e = function() {
for (O.clearRect(0, 0, L, B), T = [{
x: 0,
y: .7 * B + C
}, {
x: 0,
y: .7 * B - C
}]; T[1].x < L + C;) t(T[0], T[1])
}, t = function(e, t) {
O.beginPath(), O.moveTo(e.x, e.y), O.lineTo(t.x, t.y);
var n = t.x + (2 * I() - .25) * C,
 r = a(t.y);
O.lineTo(n, r), O.closePath(), N -= S / -50, O.fillStyle = "#" + (127 * A(N) + 128 << 16 | 127 * A(N + S / 3) + 128 << 8 | 127 * A(N + S / 3 * 2) + 128).toString(16), O.fill(), T[0] = T[1], T[1] = {
 x: n,
 y: r
}
}, a = function n(e) {
var t = e + (2 * I() - 1.1) * C;
return t > B || t < 0 ? n(e) : t
};
Y = document.getElementById("evanyou"), O = Y.getContext("2d"), E = window.devicePixelRatio || 1, L = window.innerWidth, B = window.innerHeight, C = 90, z = Math, N = 0, S = 2 * z.PI, A = z.cos, I = z.random, Y.width = L * E, Y.height = B * E, O.scale(E, E), O.globalAlpha = .6, document.onclick = e, document.ontouchstart = e, e()
}()

   
$("#toc-eye").click(function(){
$("#toc.toc-article").toggle(1000);
});

</script>


   
  <div class="text-center donation">
    <div class="inner-donation">
      <span class="btn-donation">支持一下</span>
      <div class="donation-body">
        <div class="tip text-center">扫一扫，支持poetries</div>
        <ul>
        
          <li class="item">
            
              <span>微信扫一扫</span>
            
            <img src="/images/weixin.jpg" alt="">
          </li>
        
          <li class="item">
            
              <span>支付宝扫一扫</span>
            
            <img src="/images/zhifubao.jpg" alt="">
          </li>
        
        </ul>
      </div>
    </div>
  </div>


   
  <div class="box-prev-next clearfix">
    <a class="show pull-left" href="/2017/10/21/es6-review/">
        <i class="icon icon-angle-left"></i>
    </a>
    <a class="show pull-right" href="/2017/10/23/restful-api/">
        <i class="icon icon-angle-right"></i>
    </a>
  </div>




</div>


  <a id="backTop" class="back-top">
    <i class="icon-angle-up"></i>
  </a>




  <div class="modal" id="modal">
  <span id="cover" class="cover hide"></span>
  <div id="modal-dialog" class="modal-dialog hide-dialog">
    <div class="modal-header">
      <span id="close" class="btn-close">关闭</span>
    </div>
    <hr>
    <div class="modal-body">
      <ul class="list-toolbox">
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/archives/"
              rel="noopener noreferrer"
              target="_self"
              >
              博客
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/categories/"
              rel="noopener noreferrer"
              target="_self"
              >
              分类
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/tags/"
              rel="noopener noreferrer"
              target="_self"
              >
              标签
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/search/"
              rel="noopener noreferrer"
              target="_self"
              >
              搜索
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/link/"
              rel="noopener noreferrer"
              target="_self"
              >
              友链
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/about/"
              rel="noopener noreferrer"
              target="_self"
              >
              关于
            </a>
          </li>
        
      </ul>

    </div>
  </div>
</div>



  
      <div class="fexo-comments comments-post">
    

    

    
    

    

    
    

    

<!-- Gitalk评论插件通用代码 -->
<div id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script>
const gitalk = new Gitalk({
  clientID: '5567a2c4abb858009d96',
  clientSecret: 'b9039ec056cf5c2346b3cdb63308a28c163f91e5',
  repo: 'poetries.github.io',
  owner: 'poetries',
  // 在这里设置一下截取前50个字符串, 这是因为 github 对 label 的长度有了要求, 如果超过
  // 50个字符串则会报错.
  // id: location.pathname.split('/').pop().substring(0, 49),
  id: location.pathname,
  admin: ['poetries'],
  // facebook-like distraction free mode
  distractionFreeMode: false
})
gitalk.render('gitalk-container')
</script>
<!-- Gitalk代码结束 -->



  </div>

  

  <script type="text/javascript">
  function loadScript(url, callback) {
    var script = document.createElement('script')
    script.type = 'text/javascript';

    if (script.readyState) { //IE
      script.onreadystatechange = function() {
        if (script.readyState == 'loaded' ||
          script.readyState == 'complete') {
          script.onreadystatechange = null;
          callback();
        }
      };
    } else { //Others
      script.onload = function() {
        callback();
      };
    }

    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  window.onload = function() {
    loadScript('/js/bundle.js?235683', function() {
      // load success
    });
  }
</script>


  <!-- 页面点击小红心 -->
  <script type="text/javascript" src="/js/clicklove.js"></script>
 
  
</body>
</html>
